import request from '@/utils/request'

/**
 * 
 * @param {*} c_id 类型ID
 * @param {*} userid 文件拥有者ID
 * @param {*} file 文件名称
 * @returns 
 */
export const A_downLoadFile = async function(c_id, userid, file) {
    let res = await request({
        method: 'post',
        url: '/SciThesis/downLoadFile',
        data: {
            articalCatogory: c_id,
            articalId: userid,
            articalTcatogory: file,
        },
        responseType: "blob",
    })
    return res
}



/**
 * 
 * @param {*} file 文件名称
 * @param {*} res Promise返回值
 */
export const A_showFile = function(file, res) {
    console.log(file)
    const ispdf = file.slice(file.length - 3, file.length) === "pdf";
    
    if (ispdf) {
        const url = window.URL.createObjectURL(
            new Blob([res], { type: "application/pdf" })
        );
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", `${file}`);
        document.body.appendChild(link);
        link.click();
    } else {
        const url = window.URL.createObjectURL(new Blob([res]));
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", `${file}`);
        document.body.appendChild(link);
        link.click();
    }
}



/**
 * 获取Key值
 * @param {*} c_id 类型ID
 * @returns 
 */
export const A_getKey = async function(c_id) {
    let result = await request({
        method: "post",
        url: `/SciThesis/getKey?Key=${c_id}`,
    })
    return result
}